package main

import (
	"fmt"
	"github.com/ShiYY/zinx/ziface"
	"github.com/ShiYY/zinx/znet"
)

// 基于zinx框架来开发的 服务器应用程序

func main() {
	// 1. 创建一个server句柄, 使用zinx的api
	s := znet.NewServer("[zinx v0.3]")
	// 2. 给当前zinx框架添加一个自定义的router
	s.AddRouter(&PingRouter{})
	// 3. 启动Server
	s.Server()
}

// ping test 自定义路由
type PingRouter struct {
	znet.BaseRouter
}

func (this *PingRouter) PreHandler(request ziface.IRequest) {
	fmt.Println("Call Router PreHandler...")
	_, err := request.GetConnection().TCPConnection().Write([]byte("before ping...\n"))
	if err != nil {
		fmt.Println("call back before ping error")
	}
}

func (this *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call Router Handler...")
	_, err := request.GetConnection().TCPConnection().Write([]byte("do ping...\n"))
	if err != nil {
		fmt.Println("call back do ping error")
	}
}

func (this *PingRouter) PostHandler(request ziface.IRequest) {
	fmt.Println("Call Router PostHandler...")
	_, err := request.GetConnection().TCPConnection().Write([]byte("after ping...\n"))
	if err != nil {
		fmt.Println("call back after ping error")
	}
}
